package main

import (
	"os"
	"os/signal"
	"syscall"
	"workmanager"
	"log"
	"runtime"
	"flag"
	"fmt"
	"strconv"
)

//func workflow(ch chan bool){
//
//	select {
//
//	}
//}


func main(){
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	//set logfile Stdout
	logFile, logErr := os.OpenFile("./notify.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if logErr != nil {
		fmt.Println("Fail to find", *logFile, "cServer start Failed")
		os.Exit(1)
	}
	log.SetOutput(logFile)
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	//write log
	log.Printf("Server abort! Cause:%v \n", "test log file")

	brokerList := os.Getenv("GWWF_BROKERLIST")
	topic := os.Getenv("GWWF_TOPIC")
	workdir := os.Getenv("GWWF_WORKDIR")
	frequency,err := strconv.Atoi(os.Getenv("FREQUENCY"))
	if err != nil{
		frequency = 1
	}
	wm := workmanager.NewWorkManager(8,workdir,brokerList,topic,frequency)

	go wm.Start()
	sig := make(chan os.Signal,4)
	signal.Notify(sig,syscall.SIGINT,syscall.SIGTERM,os.Interrupt)
	//go func() {
	//	time.Sleep(20*time.Second)
	//	syscall.Kill(os.Getpid(),syscall.SIGTERM)
	//}()
	<- sig
	fmt.Println("Wait for file process done...")
	wm.Stop()
}
